﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using D2DEngine.Scene;
using Microsoft.Xna.Framework.Graphics;
using D2DEngine.Util;

namespace D2DEngine
{
    public class D2DTextComponent : D2DComponent
    {
        protected D2DSceneObject sceneObject; 

        protected SpriteFont font;
        protected Color color = Color.White;
        protected float scale = 1.0f;
        protected Vector2 origin = Vector2.Zero;

        protected String text;

        public String Text 
        {
            get { return this.text; }
            set { this.text = value; }
        }

        /// <summary>
        /// The texture of the material
        /// </summary>
        public SpriteFont Font
        {
            get { return this.font; }
            set { this.font = value; }
        }

        /// <summary>
        /// The scale of the material
        /// </summary>
        public float Scale
        {
            get { return this.scale; }
            set { this.scale = value; }
        }        

        /// <summary>
        /// The color of the material
        /// </summary>
        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        /// <summary>
        /// The origin of the material
        /// </summary>
        public Vector2 Origin
        {
            get { return this.origin; }
            set { this.origin = value; }
        }  

        /// <summary>
        /// The material's width in pixel
        /// </summary>
        public float Width
        {
            get { return this.font.MeasureString(this.text).X; }
        }

        /// <summary>
        /// The material's height in pixel
        /// </summary>
        public float Height
        {
            get { return this.font.MeasureString(this.text).Y; }
        }

        /// <summary>
        /// The source rectangle of the material
        /// </summary>
        public virtual Rectangle? SourceRect
        {
            get { return null; }
        }

        /// <summary>
        /// Creates a TextComponent
        /// </summary>
        /// <param name="text">The text to display</param>
        /// <param name="?">The font</param>
        public D2DTextComponent(String text, SpriteFont font)
        {
            this.text = text;
            this.font = font;
        }

        /// <summary>
        /// Creates a TextComponent
        /// </summary>
        /// <param name="text">The text to display</param>
        /// <param name="font">The font</param>
        /// <param name="color">The text's color</param>
        /// <param name="scale">The text's size</param>
        public D2DTextComponent(String text, SpriteFont font, Color color, int scale)
        {
            this.text = text;
            this.font = font;
            this.color = color;
            this.scale = scale;
        }

        /// <summary>
        /// TextComponent default constructor
        /// </summary>
        public D2DTextComponent()
        {
        }

        /// <summary>
        /// Describes what happens when the scene object 
        /// is registered to a scene
        /// </summary>
        public override void OnRegister()
        {
            this.sceneObject = this.owner as D2DSceneObject;     
     
            if( this.origin == Vector2.Zero )
                this.origin = new Vector2(Width / 2, Height / 2);

            base.OnRegister();
        }

        /// <summary>
        /// Updates the component
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
           // this.sceneObject.BoundingBox = new Rectangle(
              //  (int)(this.sceneObject.Position.X - this.origin.X),
              //  (int)(this.sceneObject.Position.Y - this.origin.Y),
             //   Width,
              //  Height);

            base.Update(gameTime);
        }

        /// <summary>
        /// Draws the component
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (this.sceneObject.Visible)
            {
                spriteBatch.DrawString(
                    this.font,
                    this.text,
                    D2DMath.ToPixels(this.sceneObject.Position),
                    this.color,
                    this.sceneObject.Rotation,
                    this.origin,
                    this.scale,
                    SpriteEffects.None,
                    this.sceneObject.Layer / 10);
            } 

            base.Draw(spriteBatch);
        }

        /// <summary>
        /// Copies the component
        /// </summary>
        /// <param name="component"></param>
        protected override void CopyTo(D2DComponent component)
        {
            base.CopyTo(component);

            D2DTextComponent comp = (D2DTextComponent)component;
            comp.Text = this.text;
            comp.Font = this.font;
            comp.Scale = this.scale;
            comp.Color = this.color;
            comp.Origin = this.origin;
        }

        
    }
}
